Location_();
var user_name = getCookie("user_name");
$('.user_name').text(user_name)
//点击查看返回按钮
$('.return_bth').click(function() {
	$('.view_pop').css('display', 'none')
})

//点击新建线索按钮
$('.add_clues_index').click(function() {
	$('.add_clues').css("display", 'block')
})

//点击导入留言数据
$('.import_msg').click(function() {
	$('.Import_model').css('display', 'block');
})

//点击导入留言数据取消安妮
$('.return_add_clues').click(function() {
	$('.Import_model').css('display', 'none');
})

//点击导出数据
$('.export_msg').click(function() {
	$('.export_model').css('display', 'block');
})

//点击导出取消按钮
$('.return_remove_clues').click(function() {
	$('.export_model').css('display', 'none');
})

//点击线索分配cancel按钮
$('.cancel').click(function() {
	$('.Allcation').css('visibility', 'hidden');
	$('.Allcation .content').css('transform', 'scale(0)');
	$('#owner_name').val("");
})

//点击选择联系人Cancel按钮
$('.Cancel').click(function() {
	$('.Trace_leader').css('visibility', 'hidden');
	$('.Trace_leader .content').css('transform', 'scale(0)');
})

//点击admin框
$('#owner_name').click(function() {
	$('.Trace_leader').css('visibility', 'visible');
	$('.Trace_leader .content').css('transform', 'scale(1)');
})

layui.use('element', function() {
	var element = layui.element;
	//一些事件监听
	element.on('tab(tabdemo)', function(data) {});
})

layui.use(['jquery', 'layer'], function() {
	var $ = layui.$ //重点处
		,
		layer = layui.layer;
});

//网谈table
var departent_name;
var role_id;
var select_name = $('#select_name');
var urls = "http://" + com_url + "//admin/talk";
var message_url = "http://" + com_url + "//admin/message";
var batch_url = "http://" + com_url + "//admin/batch";
layui.use(['table', 'form'], function() {
	var table = layui.table;
	var $ = layui.$
	var form = layui.form;

	//	//监听工具条
	var clue_id_Array = [];
	var allcation_id = [];

	function table_render(url, callback) {
		table.render({
			elem: '#LAY_table_clue',
			url: url,
			response: {
				statusCode: 200
			},
			cols: [
				[{
						fixed: 'left',
						checkbox: true,
					}, {
						field: 'clue_project',
						width: '8%',
						title: '咨询项目',
						event:'allcations'
						
					}, {
						field: 'clue_area',
						width: '6%',
						title: '咨询地',
						event:'allcation'
					}, {
						field: 'clue_number',
						width: '7%',
						title: '当天资源序号',
						event:'allcation'
					}, {
						field: 'clue_unit',
						width: '6%',
						title: '下发部门',
						event:'allcation'
					}, {
						field: 'category',
						width: '6%',
						title: '分类',
						event:'allcation'
					}, {
						field: 'clue_source',
						width: '6%',
						title: '来源',
					}, {
						field: 'clue_platform',
						width: '6%',
						title: '平台',
					}, {
						field: 'clue_key',
						width: '12%',
						title: '关键词',
					}, {
						field: 'create_time',
						width: '12.8%',
						title: '资源时间'
					},
					{
						field: 'distribution_time',
						width: '12.8%',
						title: '下发时间'
					}, {
						field: 'owner_name',
						width: '6%',
						title: '负责人'
					}, {
						field: 'right',
						width: '8.2%',
						toolbar: '#barDemo1',
						title: '操作'
					}
				]
			],
			page: true,
			limit: 10,
			id: 'testreload',
			done: function(res, count) { //回调函数
				var perm = res.perm;
				var code = res.code;
				var msg = res.msg;
				if(code == 204) {
					clearCookie();
					window.parent.top.location.reload();
				}
				if(code == "200") {
					$.each(perm, function(index, res) { //遍历  
						if(res.title == "网谈") { //网谈
							$('#wangtan').removeClass('hide');
						}
						if(res.title == "留言") { //网谈
							$('.liuyan').removeClass('hide');
						}
						if(res.title == "网谈" && res.title == "留言") { //网谈
							$('#wangtan').removeClass('hide');
							$('#leave_msg').removeClass('layui-this')

						}
					});

				} else {
					layer.msg(msg, {
						icon: 0,
						time: 3500
					});
				}
				// if(code == '203') {
				// 	layer.msg('抱歉，您没有此权限', {
				// 		icon: 0,
				// 		time: 3500
				// 	});
				// }
			}
		});
	}
	
	table.on('row(LAY_table_clue)', function(obj) {
		var data = obj.data;
		console.log("fdfd")	

		//标注选中样式
		obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
	});
	//	setInterval(function(){
	$.ajax({
		type: "get",
		beforeSend: cookie_val_fun,
		url: "http://" + com_url + "//admin/getClueLog",
		success: function(res) {
			var data = JSON.parse(res).data;
			$.each(data, function(index, curr) {
				var $tr = $("<li>" +
					"<span>第</span>" +
					"<span class='record_num'>" + curr.wt_number + "</span>" +
					"<span>条-</span>" +
					"<span class='record_time'>" + curr.time + "</span>" +
					"<span>--</span>" +
					"<span class='record_Name'>" + curr.username + "</span>" +
					+"</li>");
				$('#ceshi').append($tr)
			});

		}
	});
	//	},6000)

	table.on('tool(leaderss)', function(obj) {
		var data = obj.data;
		var datas = JSON.parse(JSON.stringify(data));
		if(obj.event === 'detail') {
			$('.view_pop').css('display', 'block')
		} else if(obj.event === 'edit') { //领取功能
			var idss = datas.clue_id;
			clue_id_Array.push(idss);
			var clue_id_Array_qc = $.unique(clue_id_Array);
			var demoReload_val = $('#demoReload').val();
			var index = layer.load(1, {
				shade: [0.5, '#fff'] //0.1透明度的白色背景
			});
			$.ajax({
				type: "post",
				url: "http://" + com_url + "//admin/receive",
				data: {
					clue_id: clue_id_Array_qc,
				},
				beforeSend: function(XMLHttpRequest) {
					XMLHttpRequest.setRequestHeader("ApiAuth", cookie_val);
				},
				success: function(res) {
					var Ress = JSON.parse(res);
					var code = Ress.code;
					var msg = Ress.msg;
					layer.close(index);
					if(code == '200') {
						obj.del();
						clue_id_Array = [];
						var demoReload = $('#demoReload');
						setTimeout(function() {
							layer.msg('领取成功', {
								icon: 1,
								time: 1500
							});
						}, 500)
						setTimeout(function() {
							localStorage.setItem("Herf_id", '我的客户'); //放入session
							window.parent.top.location.reload();
						}, 1200)
						$(".layui-laypage-btn")[0].click();
					} else {
						clue_id_Array = [];
						layer.msg(msg, {
							icon: 2,
							time: 1500
						});
					}
				},
				error: function(res) {
					alert("数据库异常");
				}
			});
		} else if(obj.event === 'allcation') {
			$('.Allcation').css('visibility', 'visible');
			$('.Allcation .content').css('transform', 'scale(1)');
			var allcation_ids = datas.clue_id;
			allcation_id.push(allcation_ids);
		}
	});
	active = {
		//手机号搜索
		reload: function() {
			var index = layer.load(1, {
				shade: [0.5, '#fff'] //0.1透明度的白色背景
			});
			var demoReload = $('#demoReload');
			var sourc_select = $('#sourc_select').val();
			//执行重载
			table.reload('testreload', {
				page: {
					curr: 1 //重新从第 1 页开始
				},
				where: {
					phone: demoReload.val(),
					source: sourc_select
				},
				done: function(res) {
					layer.close(index);
				}
			});
		},
		//批量领取
		getCheckData: function(obj) { //获取选中数据
			var checkStatus = table.checkStatus('testreload'),
				data = checkStatus.data;
			var datass = JSON.parse(JSON.stringify(data));
			var Batch_recevie_Array = [];
			for(var i = 0; i < datass.length; i++) {
				var idss = datass[i].clue_id;
				Batch_recevie_Array.push(idss);
			};
			var Batch_recevie_Array_qc = $.unique(Batch_recevie_Array);
			if(Batch_recevie_Array_qc == "") {
				layer.msg('未勾选资源', {
					icon: 0
				});
				return false;
			}
			var index = layer.load(1, {
				shade: [0.5, '#fff'] //0.1透明度的白色背景
			});
			$.ajax({
				type: "post",
				url: "http://" + com_url + "//admin/receive",
				data: {
					clue_id: Batch_recevie_Array_qc,
				},
				beforeSend: function(XMLHttpRequest) {
					XMLHttpRequest.setRequestHeader("ApiAuth", cookie_val);
				},
				success: function(data) {
					// if(data == "") {} else {
					var datass = JSON.parse(data);
					var code = datass.code;
					var msg = datass.msg;
					layer.close(index);
					if(code == '200') {
						var demoReload = $('#demoReload');
						$(".layui-laypage-btn")[0].click();
						layer.msg('领取成功', {
							icon: 1,
							time: 2000
						});
						setTimeout(function() {
							window.location.href = "MyUser.html";
						}, 1200)
					} else {
						layer.msg(msg, {
							icon: 2,
							time: 1500
						});
					}
					// }
				},
				error: function(res) {
					alert("数据库异常");
				}
			});
		},
		//批量分配
		distribution: function(obj) {
			var checkStatus = table.checkStatus('testreload'),
				data = checkStatus.data;
			var datass = JSON.parse(JSON.stringify(data));
			if(datass == "") {
				layer.msg('未勾选资源', {
					icon: 0
				});
				return false;
			};
			var testpush = [];
			for(var i = 0; i < datass.length; i++) {
				var idss = datass[i].clue_id;
				allcation_id.push(idss);
			};
			$('.Allcation').css('visibility', 'visible');
			$('.Allcation .content').css('transform', 'scale(1)');
		},
		//网谈传参
		wangtan_btn: function() {
			table_render(urls)
		},
		//留言传参
		leaves_bth: function() {
			table_render(message_url)
		},
	};
	//电话搜索
	$('.demoTable #search_val').on('click', function() {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});
	//批量领取
	$('#search_vals').on('click', function() {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});
	//批量分配
	$('#mass_distribution').on('click', function() {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});
	//点击负责人人搜索
	$('#admin_seach').on('click', function() {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});
	//点击留言
	$('.layui-tab-title li').on('click', function() {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});
	//	自动触发事件
	if(getPicTabNum) {
		$('.layui-tab-title li').eq(getPicTabNum).trigger('click')
	}
	$('.layui-tab-title .liuyan').on('click', function() {
		$('.source_msg').removeClass('hide')
	});
	$('.layui-tab-title .wangtan').on('click', function() {
		$('.source_msg').addClass('hide')
	});

	//监听单元格事件
	var user_names;
	table.on('tool(leader)', function(obj) {
		var data = obj.data;
		var datas = JSON.parse(JSON.stringify(data))
		if(obj.event === 'setSign') {
			role_id = datas.role_id;
			var position_name = datas.position_name;
			user_names = datas.name;
		}
	});
	//dian
	$('.OkBtn').click(function() {
		$('#owner_name').val(user_names);
		if($('#owner_name').val()) {
			$('.Trace_leader').css('visibility', 'hidden');
			$('.Trace_leader .content').css('transform', 'scale(0)');
		} else {
			layer.msg("未选择联系人", {
				icon: 0
			})
		}
	})

	//ok提交
	$('#submit_id').on('click', function() {
		var role_id_arry = [];
		var owner_name = $('#owner_name').val();
		var unime_allcation = unique1(allcation_id);
		role_id_arry.push(role_id);
		if(owner_name == "") {
			layer.msg("未选择联系人", {
				icon: 0
			})
			return false
		};
		var index_shade = layer.load(1, {
			shade: [0.5, '#fff'] //0.1透明度的白色背景
		});
		$.ajax({
			type: "post",
			url: "http://" + com_url + "//admin/distribution",
			data: {
				assigner: role_id, //选择人的id
				clue_id: unime_allcation, //选中行的id   数组形式传送
			},
			beforeSend: function(XMLHttpRequest) {
				XMLHttpRequest.setRequestHeader("ApiAuth", cookie_val);
			},
			success: function(data) {
				var datass = JSON.parse(data);
				var code = datass.code;
				var msg = datass.msg;
				var demoReload = $('#demoReload');
				layer.close(index_shade);
				if(code == "200") {
					allcation_id = [];
					role_id_arry = [];
					$('.Allcation').css('visibility', 'hidden');
					$('.Allcation .content').css('transform', 'scale(0)');
					$(".layui-laypage-btn")[0].click();
					layer.msg("分配成功", {
						icon: 1
					})
				} else {
					clue_id_Array = [];
					layer.msg(msg, {
						icon: 0,
						time: 1500
					});
				}
			},
			error: function(res) {
				alert("分配失败");
			}
		});
	});
});